package top.hmtools.ckfinder.beans.commons;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * 最基本的、抽象的“指令”执行结果返回数据类型统一父类
 * 
 * @author Hybomyth
 *
 */
public abstract class BaseResultBean {
	
	@XmlAttribute(name="resourceType")
	protected String resourceType;

	@XmlElement(name="Error")
	protected ErrorBean errorBean;

	@XmlTransient
	public ErrorBean getErrorBean() {
		return errorBean;
	}

	public void setErrorBean(ErrorBean errorBean) {
		this.errorBean = errorBean;
	}
	
	@XmlTransient
	public String getResourceType() {
		return resourceType;
	}

	public void setResourceType(String resourceType) {
		this.resourceType = resourceType;
	}

	public String toXMLString() throws JAXBException{
		JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass());
		Marshaller marshaller = jaxbContext.createMarshaller();
		
		//以下是为生成xml做的一些配置
		//格式化输出，即按标签自动换行，否则就是一行输出
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		//设置编码（默认编码就是utf-8）
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		//是否省略xml头信息，默认不省略（false）
		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
		
		StringWriter resultStr=new StringWriter();
		marshaller.marshal(this, resultStr);
		return resultStr.toString();
	}
}
